package com.kaifamiao.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author lucien-ma
 * @date 2021/8/27
 * @apiNote
 */
public class MyCGLIBProxy implements MethodInterceptor {

    private Object obj = null;

    public Object bind(Object obj) {
        this.obj = obj;
        Object object =  Enhancer.create(obj.getClass(),this);
        return object;
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("cglib 代理模式");
        System.out.println("执行了额外的扩展功能....");
        return proxy.invokeSuper(obj, args);
    }

    public static void main(String[] args) {
        MyCGLIBProxy myCGLIBProxy = new MyCGLIBProxy();
        Dog dh = (Dog) myCGLIBProxy.bind(new Dog());
        dh.run();
    }
}
